//
//  MineViewController.m
//  FitTime
//
//  Created by 赵建平 on 16/3/30.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "MineViewController.h"
#import "DownLoadViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <MessageUI/MessageUI.h>
#import "CleanCaches.h"

@interface MineViewController ()<MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) UITextField *nameField;
@property (nonatomic, strong) NSString *cacheString;

@end

@implementation MineViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    
    backImageView.image = [UIImage imageNamed:@"樱花树.jpg"];
    
    [self.view addSubview:backImageView];
    
    [self createButton];
    
    // =================== 樱花飘落 ====================
    CAEmitterLayer * snowEmitterLayer = [CAEmitterLayer layer];
    snowEmitterLayer.emitterPosition = CGPointMake(100, -30);
    snowEmitterLayer.emitterSize = CGSizeMake(self.view.bounds.size.width * 2, 0);
    snowEmitterLayer.emitterMode = kCAEmitterLayerOutline;
    snowEmitterLayer.emitterShape = kCAEmitterLayerLine;
    //    snowEmitterLayer.renderMode = kCAEmitterLayerAdditive;
    
    CAEmitterCell * snowCell = [CAEmitterCell emitterCell];
    snowCell.contents = (__bridge id)[UIImage imageNamed:@"樱花瓣"].CGImage;
    
    // 花瓣缩放比例
    snowCell.scale = 0.04;
    snowCell.scaleRange = 0.6;
    
    // 每秒产生的花瓣数量
    snowCell.birthRate = 20;
    snowCell.lifetime = 40;
    
    // 每秒花瓣变透明的速度
    snowCell.alphaSpeed = -0.01;
    
    // 秒速“五”厘米～～
    snowCell.velocity = 100;
    snowCell.velocityRange = 200;
    
    // 花瓣掉落的角度范围
    snowCell.emissionRange = M_PI;
    
    // 花瓣旋转的速度
    snowCell.spin = M_PI_4;
    
    // 每个cell的颜色
    //    snowCell.color = [[UIColor redColor] CGColor];
    
    // 阴影的 不透明 度
    snowEmitterLayer.shadowOpacity = 1;
    // 阴影化开的程度（就像墨水滴在宣纸上化开那样）
    snowEmitterLayer.shadowRadius = 8;
    // 阴影的偏移量
    snowEmitterLayer.shadowOffset = CGSizeMake(3, 3);
    // 阴影的颜色
    snowEmitterLayer.shadowColor = [[UIColor whiteColor] CGColor];
    
    
    snowEmitterLayer.emitterCells = [NSArray arrayWithObject:snowCell];
    [self.view.layer addSublayer:snowEmitterLayer];
    
}

- (void)createButton {
    
    QBFlatButton *downloadButton = [QBFlatButton buttonWithType:UIButtonTypeCustom];
    downloadButton.frame = CGRectMake(50 * SCALEWIDTH, 130 * SCALEHEIGHT, 275 * SCALEWIDTH, 60 * SCALEHEIGHT);
    downloadButton.surfaceColor = [UIColor colorWithRed:0.888 green:0.460 blue:0.615 alpha:0.997];
    
    downloadButton.sideColor = [UIColor colorWithRed:0.702 green:0.407 blue:0.674 alpha:1.000];
    downloadButton.cornerRadius = 8.0;
    downloadButton.height = 4.0;
    downloadButton.depth = 3.0;
    
    downloadButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [downloadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [downloadButton setTitle:@"跑步说明" forState:UIControlStateNormal];
    
    [self.view addSubview:downloadButton];

    [downloadButton addTarget:self action:@selector(downloadAction:) forControlEvents:UIControlEventTouchUpInside];
    
    QBFlatButton *cacheButton = [QBFlatButton buttonWithType:UIButtonTypeCustom];
    cacheButton.frame = CGRectMake(50 * SCALEWIDTH, 220 * SCALEHEIGHT, 275 * SCALEWIDTH, 60 * SCALEHEIGHT);
    cacheButton.surfaceColor = [UIColor colorWithRed:0.888 green:0.460 blue:0.615 alpha:0.997];
    
    cacheButton.sideColor = [UIColor colorWithRed:0.702 green:0.407 blue:0.674 alpha:1.000];
    cacheButton.cornerRadius = 8.0;
    cacheButton.height = 4.0;
    cacheButton.depth = 3.0;
    
    cacheButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [cacheButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cacheButton setTitle:@"清除缓存" forState:UIControlStateNormal];
    
    [self.view addSubview:cacheButton];
    
    [cacheButton addTarget:self action:@selector(cacheAction) forControlEvents:UIControlEventTouchUpInside];
    
    QBFlatButton *feedbackButton = [QBFlatButton buttonWithType:UIButtonTypeCustom];
    feedbackButton.frame = CGRectMake(50 * SCALEWIDTH, 310 * SCALEHEIGHT, 275 * SCALEWIDTH, 60 * SCALEHEIGHT);
    feedbackButton.surfaceColor = [UIColor colorWithRed:0.888 green:0.460 blue:0.615 alpha:0.997];
    
    feedbackButton.sideColor = [UIColor colorWithRed:0.702 green:0.407 blue:0.674 alpha:1.000];
    feedbackButton.cornerRadius = 8.0;
    feedbackButton.height = 4.0;
    feedbackButton.depth = 3.0;
    
    feedbackButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [feedbackButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [feedbackButton setTitle:@"问题反馈" forState:UIControlStateNormal];
    
    [self.view addSubview:feedbackButton];
    
    [feedbackButton addTarget:self action:@selector(feedbackAction) forControlEvents:UIControlEventTouchUpInside];
    

}

#pragma mark 下载
- (void)downloadAction:(UIButton *)button {
    
    UIAlertView *alartView = [[UIAlertView alloc]initWithTitle:nil message:@"运动语音可以搜索天气，如“大连天气” 还可以控制运动类型 还可以搜索关键字“老王”" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    

    [alartView show];

}

#pragma mark 清除缓存
- (void)cacheAction {
    
    self.cacheString = [NSString stringWithFormat:@"清理缓存%@", [CleanCaches folderSizeAtPath]];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:_cacheString preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        _cacheString = [CleanCaches cleanCachesAtPath];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
    [self presentViewController:alert animated:true completion:nil];

}

#pragma mark 意见反馈
- (void)feedbackAction {
    
    UIAlertView * customAlertView = [[UIAlertView alloc] initWithTitle:@"请反馈您宝贵的意见" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

    [customAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
    self.nameField = [customAlertView textFieldAtIndex:0];
    
    _nameField.placeholder = @"您的意见";
    
    [customAlertView show];
    
}
#pragma mark 发送邮件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        NSLog(@"意见反馈");
        if (_nameField.text.length != 0) {
            //发送邮件
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
            //设置主题
            [picker setSubject:@"ShowTiming-问题反馈"];
            
            //设置收件人
            NSArray *toRecipients = [NSArray arrayWithObjects:@"411429550@qq.com",
                                     nil];
      
            [picker setToRecipients:toRecipients];
       
            // 设置邮件发送内容
            NSString *emailBody = self.nameField.text;
            [picker setMessageBody:emailBody isHTML:YES];

            [self presentModalViewController: picker animated:YES];
        }
       
    } else {
        
        [_nameField resignFirstResponder];
        
    }
    
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];

    switch (result)
    {
        case MFMailComposeResultCancelled: //取消
            NSLog(@"MFMailComposeResultCancelled-取消");
            break;
        case MFMailComposeResultSaved: // 保存
            NSLog(@"MFMailComposeResultSaved-保存邮件");
            break;
        case MFMailComposeResultSent: // 发送
            NSLog(@"MFMailComposeResultSent-发送邮件");
            break;
        case MFMailComposeResultFailed: // 尝试保存或发送邮件失败
            NSLog(@"MFMailComposeResultFailed: %@...",[error localizedDescription]);
            break;
    }
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
